import java.io.*;

// Imdb encoding: ISO-8859-1
class CachedFileWriter
{
  CachedFileWriter(String filePath, String encoding)
  {
    ready = false;
    theFile = new File(filePath);
    try {
      FileOutputStream fos = new FileOutputStream(theFile);
      OutputStreamWriter out = encoding == null ? new OutputStreamWriter(fos) : new OutputStreamWriter(fos, encoding);
      output = new BufferedWriter(out, BUFFER_SIZE);
      ready = true;
    } catch (Exception e)
    {
      println("ERROR in opening file: " + e.getMessage());
    }
  }
  
  boolean isReady()
  {
    return ready;
  }
  
  void writeLine(String theLine)
  {
    try {
      output.write(theLine);
      output.newLine();
    } catch (Exception e) {
      println("ERROR in write: " + e.getMessage());
    }
  }
  
  void writeString(String txt)
  {
    try {
      output.write(txt);
    } catch (Exception e) {
      println("ERROR in write: " + e.getMessage());
    }
  }
  
  void flushFile()
  {
    try {
      output.flush();
    } catch (Exception e) {
      println("ERROR in closeFile: " + e.getMessage());
    }
  }
  
  void closeFile()
  {
    try {
      output.close();
    } catch (Exception e) {
      println("ERROR in closeFile: " + e.getMessage());
    }
  }
  
  private boolean ready;
  private File theFile;
  private BufferedWriter output;
}
